<?php defined('SYSPATH') or die('No direct script access.');
/**
 *
 * 采购付款单批量上传
 * @author locove
 *
 */
class Controller_Ebaysales extends Controller
{
    /**
     * 描述 : ebay销量统计
     * 作者 : Edgar.lee
     */
    public function action_index() {
        if( PHP_SAPI !== 'cli' ) return;
        set_time_limit(0);                                                                                              //防超时
        ignore_user_abort(true);

        $count = 0;                                                                                                     //导入计数
        $maxDate = date('Y-m-d', $_SERVER['REQUEST_TIME']);
        $nowDate = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
        echo "交期导入开始 : {$nowDate} \n";

        $sql = 'SELECT
            UNIX_TIMESTAMP(`add_time`) maxDate
        FROM
            `ebay_sales`
        ORDER BY
            `id` DESC 
        LIMIT 1';
        $minDate = DB::query(Database::SELECT, $sql)->execute('newerp')->as_array();                                    //读取数据库最大时间
        if(empty($minDate))
        {
            $minDate = date('Y-m-d',$_SERVER['REQUEST_TIME'] - 2592000);
            $minRange = date('Y-m-d',$minDate - 172800);
        }else{
            $minDate = date('Y-m-d',$minDate[0]['maxDate']);
            $minRange = date("Y-m-d",$_SERVER['REQUEST_TIME'] - 2592000);
        }

        /*$minDate = empty($minDate) ? $_SERVER['REQUEST_TIME'] - 1728000 : $minDate[0]['maxDate'];                       //生成最小范围的过滤条件
        $minRange = date('Y-m-d', $minDate - 2592000);                                                                  //最小日期格式
        $minDate = date('Y-m-d', $minDate);*/

        $sql = "SELECT
            `pre`
        FROM
            `table_pres`
        WHERE
            `created_time` >= '{$minRange}'";
        $temp = DB::query(Database::SELECT, $sql)->execute('ol_account')->as_array();                                   //读取数据库最大时间

        /**
         *  	2 	中美专线 	CS_
                4 	中英专线 	CK_
                6 	中澳专线 	CU_
                8 	中德专线 	CD_
                10 	国内 	CN_
         */
        $sql = array();
        foreach($temp as &$v) {
            $sql[] = "(SELECT
                TRIM(`ebay_orderproducts{$v['pre']}`.`item_sku`) sku,                         /*商品编码*/
                SUM(`ebay_orderproducts{$v['pre']}`.`quantity_purchased`) c,            /*出货数量*/
                DATE_FORMAT(`ebay_orders{$v['pre']}`.`add_time`, '%Y-%m-%d') `date`,    /*统计时间*/
                '{$nowDate}' `addTime`                                                  /*添加时间*/
            FROM 
                `ebay_orders{$v['pre']}`
                    LEFT JOIN `ebay_orderproducts{$v['pre']}` ON
                        `ebay_orderproducts{$v['pre']}`.`order_number_id` = `ebay_orders{$v['pre']}`.`order_number_id`
            WHERE
                `ebay_orders{$v['pre']}`.`order_status` = '1'
            AND `ebay_orders{$v['pre']}`.`add_time` >= '{$minDate}'
            AND `ebay_orders{$v['pre']}`.`add_time` < '{$maxDate}'
            AND `ebay_orderproducts{$v['pre']}`.`item_sku`<>''
            AND `ebay_orderproducts{$v['pre']}`.`report_shipping_type` IN (2,4,6,8,10)
            GROUP BY
                `ebay_orderproducts{$v['pre']}`.`item_sku`, DATE_FORMAT(`ebay_orders{$v['pre']}`.`add_time`, '%Y-%m-%d'))";
        }

        $sql = join(' UNION ALL ', $sql);
        $sql = "SELECT
            `data`.sku, SUM(`data`.c) c, `data`.`date`, '{$nowDate}'
        FROM
            ({$sql}) `data`
        GROUP BY
            `data`.`sku`, `data`.`date`";

        while( tool::eachSql($sql, $data, 'ol_order') ) {
            foreach($data as &$v) {
                $v = '("' . join('","', tool::slashesDeep($v)) . '")';
                $count += 1;
            }
            $data = 'INSERT INTO `ebay_sales` (
                `sku`, `sale`, `count_date`, `add_time`
            ) VALUES ' . join(',', $data);
            DB::query(Database::INSERT, $data)->execute('newerp');
        }

        checkstock::bomb('ebay', $minDate);
        checkstock::expCn('ebay');
        checkstock::badBegin('ebay');
        checkstock::activeOut(1, $minDate);
        $nowDate = time() - $_SERVER['REQUEST_TIME'];
        echo "交期导入结束 : 导入 {$count} 条, 耗时 {$nowDate} 秒\n";
    }
}